using System;
using System.Collections;
using BitMango;
using Holoville.HOTween;
using Holoville.HOTween.Plugins;
using SevenPieceGame;
using TMPro;
using UnityEngine;

public class TopOverlayHandler : MonoBehaviour
{
	public void DoDataExchange()
	{
	}

	private void OnEnable()
	{
		if (Wallet.IsValid())
		{
			this.OnChangedItem(null, 0);
		}
		Wallet.itemChangedCallback += this.OnChangedItem;
	}

	private void OnDisable()
	{
		Wallet.itemChangedCallback -= this.OnChangedItem;
	}

	private void OnClickStore()
	{
		//if (Scene.IsPopupOpen() && Scene.CurrentPopup().name.Equals("StorePopup"))
		//{
		//	return;
		//}
		//if (Scene.IsLocked)
		//{
		//	return;
		//}
		//BMSoundHelper.Play("se_btnclick", false, 1f);
		//Scene.ShowPopup("StorePopup", null, null);
	}

	private void OnChangedItem(InventoryItem item, int count)
	{
		this.coinCount.text = ((Wallet.GetItemCount("blockcoin") != 0) ? (string.Empty + Wallet.GetItemCount("blockcoin")) : "0");
		this.Bounce(this.icon.gameObject, new Vector3(1.3f, 1.3f, 1f));
	}

	private void Bounce(GameObject go, Vector3 scale)
	{
		if (go == null)
		{
			return;
		}
		HOTween.Complete(go.GetInstanceID().ToString());
		Vector3[] p_path = new Vector3[]
		{
			Vector3.one,
			scale,
			Vector3.one
		};
		TweenParms p_parms = new TweenParms().Prop("localScale", new PlugVector3Path(p_path, false, PathType.Curved)).Ease(EaseType.EaseOutBounce, 10f).Loops(3);
		Sequence sequence = new Sequence(new SequenceParms().Id(go.GetInstanceID().ToString()));
		sequence.Append(HOTween.To(go.transform, 1f, p_parms));
		sequence.Play();
	}

	public void StartCoinGap(int getCoin)
	{
		int num = Wallet.GetItemCount("blockcoin") - getCoin;
		this.coinCount.text = ((num != 0) ? string.Format("{0}", num) : "0");
	}

	public void CoinAnimation(Vector3 position, int aniCount, float coinDelay, int getCoin, Action callback)
	{
		base.StartCoroutine(this.CoCoinAnimation(position, aniCount, coinDelay, getCoin, callback));
	}

	private IEnumerator CoCoinAnimation(Vector3 position, int aniCount, float coinDelay, int getCoin, Action callback)
	{
		int prevCoinCount = Wallet.GetItemCount("blockcoin") - getCoin;
		this.coinCount.text = ((prevCoinCount != 0) ? string.Format("{0}", prevCoinCount) : "0");
		int completeCount = 0;
		for (int i = 0; i < aniCount; i++)
		{
			CoinHandler coinWidget = Widget.Create<CoinHandler>();
			coinWidget.transform.parent = base.transform;
			coinWidget.transform.localPosition = new Vector3(position.x, position.y, -20f);
			coinWidget.name = "clearCoin";
			coinWidget.SetData(this.icon.transform.position, 1f, 0.77f, 0, delegate(int _obj)
			{
				BMSoundHelper.Play("getCoin", false, 1f);
				completeCount++;
			});
			yield return new WaitForSeconds(coinDelay);
		}
		while (completeCount != 1)
		{
			yield return new WaitForEndOfFrame();
		}
		this.num = (float)prevCoinCount;
		Sequence seq = new Sequence(new SequenceParms().Id(base.gameObject.name + "_COUNT_ANIMATION"));
		TweenParms parms = new TweenParms().Prop("num", prevCoinCount + getCoin).Ease(EaseType.EaseOutSine).OnUpdate(delegate()
		{
			this.UpdateText();
		}).OnComplete(delegate()
		{
			this.num = (float)(prevCoinCount + getCoin);
		});
		seq.Insert(0f, HOTween.To(this, 0.75f, parms));
		seq.Play();
		yield return new WaitForSeconds(1f);
		while (completeCount != 5)
		{
			yield return new WaitForEndOfFrame();
		}
		yield return new WaitForSeconds(0.2f);
		if (callback != null)
		{
			callback();
		}
		yield break;
	}

	private void OnClickShopBtn()
	{
		if (UserLayerManager.Instance.IsInstallOrLowUser())
		{
			MainManager.Instance.ShowUIShop();
		}
		else
		{
			Scene.ShowPopup("StorePopup", null, null);
		}
	}
	
	private void UpdateText()
	{
		this.coinCount.text = ((this.num != 0f) ? string.Format("{0}", (int)this.num) : "0");
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public tk2dSprite icon;

	public TextMeshPro coinCount;

	[HideInInspector]
	public float num;

	private static bool isOpenedQuestPopup;
}
